// pages/history/history.js
import * as echarts from '../../components/ec-canvas/echarts';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {},
    ec2: {},
    screenHeight: app.globalData.screenHeight,
    option1:{},
    option2:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.option1 = {
      title: {
        text: '',
        left: 'center'
      },
      color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
      grid: {
        containLabel: true,
        // left:24,
        left:60,
        top: 24,
        // right: 20,
        right:60,
        bottom: 0
      },
      tooltip: {
        show: true,
        trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['2017', '2018', '2019', '2020'],
        axisTick: {
          show: false
        },
        scale: true,
      },
      yAxis: {
        scale: false,
        x: 'center',
        type: 'value',
        splitLine: {
          lineStyle: {
            type: 'solid',
            color: ['#EEEEEE']
          },
        },
        axisTick: {
          show: false
        },
        axisLine: {
          show: false,
          onZero: false
        },
        offset:36
      },
      series: [{
        name: '招生人数',
        type: 'line',
        data: [30, 78, 400, 33],
        smooth: true,
        symbolSize: 8,
        itemStyle: {
          normal: {
            label: {
              show: true
            }
          }
        }
      }]
    };
    this.data.option2 = {
      title: {
        text: '',
        left: 'center'
      },
      color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
      grid: {
        containLabel: true,
        // left:24,
        left:60,
        top: 24,
        // right: 20,
        right:60,
        bottom: 0
      },
      tooltip: {
        show: true,
        trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['2017', '2018', '2019', '2020'],
        axisTick: {
          show: false
        },
        scale: true,
      },
      yAxis: {
        scale: false,
        x: 'center',
        type: 'value',
        splitLine: {
          lineStyle: {
            type: 'solid',
            color: ['#EEEEEE']
          },
        },
        axisTick: {
          show: false
        },
        axisLine: {
          show: false,
          onZero: false
        },
        // offset:12
        offset:36
      },
      series: [{
        name: '省控分数线',
        type: 'line',
        data: [50, 178, 400, 13],
        smooth: true,
        symbolSize: 8,
        itemStyle: {
          normal: {
            label: {
              show: true
            }
          }
        },
        color: '#2074D5'
      }, {
        name: '补录分数线',
        type: 'line',
        data: [30, 78, 4000, 33],
        smooth: true,
        symbolSize: 8,
        itemStyle: {
          normal: {
            label: {
              show: true
            }
          }
        },
        color: '#E0A02A'
      }]
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    app.globalData.wxRequest.requestGet('data/enrollmentPlan.do', {}).then(res => {
      console.log('test1',res);
      let option1 = that.data.option1
      let option2 = that.data.option2
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      let history = res.data;
      let data1 = [];
      let year1 = []
      let data2 = [];
      let year2 = []
      let data3 = [];
      let year3 = []
      history[0].data.map((item, index) => {
        data1[index] = item.num
        year1[index] = item.year
      })
      history[1].data.map((item, index) => {
        data2[index] = item.num
        year2[index] = item.year
      })
      history[2].data.map((item, index) => {
        data3[index] = item.num
        year3[index] = item.year
      })
      option1.series[0].data = data1
      option1.xAxis.data = year1
      option2.series[0].data = data2
      option2.series[1].data = data3
      option2.xAxis.data = year2
      option2.series[0].name = history[1].name
      option2.series[1].name = history[2].name
      that.setData({
        history: history,
        option1: option1,
        option2: option2
      }, function () {
        console.log('99999999999999');
        that.init_bar1(that)
        that.init_bar2(that)
      })
      console.log('test3',);
    }).catch(err => {
      console.log('test2',err);
      wx.showToast({
        title: '请求失败2222',
        icon: 'error'
      })
    })
    app.globalData.wxRequest.requestGet('data/getDataUrl.do').then(res => {
      console.log('res.data.imgUrl', res.data.imgUrl);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        imgUrl: res.data.imgUrl
      })
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showBigImg(e) {
    console.log(e);
    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.url] // 需要预览的图片http链接列表
    })
  },
  init_bar1() {
    let ecComponent1 = this.selectComponent('#mychart-dom-line');
    ecComponent1.init((canvas, width, height) => {
      // 初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      chart.setOption(this.data.option1);
      console.log('chart', chart)
      wx.hideLoading();
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },
  init_bar2() {
    let ecComponent2 = this.selectComponent('#mychart-line');
    ecComponent2.init((canvas, width, height) => {
      // 初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      chart.setOption(this.data.option2);
      wx.hideLoading();
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },
})